<template>
  <div class="related-list">
    <div v-if="title" class="section-title">{{ title }}</div>
    <div class="video-grid">
      <div v-for="item in items" :key="item.id || item.data?.id" class="video-item" @click="handleItemClick(item)">
        <div class="video-thumbnail">
          <img :src="item.data?.cover?.feed || ''" :alt="item.data?.title" />
          <div class="duration">{{ formatDuration(item.data?.duration) }}</div>
        </div>
        <div class="video-info">
          <div class="video-title">{{ item.data?.title || '' }}</div>
          <div class="video-author"> {{ item.data?.author?.name || '未知' }}</div>
          <div class="video-tags">
            <span v-for="tag in getTags(item.data)" :key="tag" class="tag">{{ tag }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
interface Cover {
  feed?: string
  detail?: string
  blurred?: string
}

interface RelatedItemData {
  id?: number
  title?: string
  category?: string
  library?: string
  duration?: number
  cover?: Cover
  playUrl?: string
  description?: string
  author?: any
  consumption?: any
  webUrl?: any
  tags?: any[]
}

interface RelatedItem {
  id?: number
  type?: string
  data?: RelatedItemData
}

const props = defineProps<{
  items?: RelatedItem[]
  title?: string
}>()

const emit = defineEmits<{
  'item-click': [item: RelatedItem]
}>()

// 格式化时长
const formatDuration = (seconds?: number) => {
  if (!seconds) return '00:00'
  const mins = Math.floor(seconds / 60)
  const secs = seconds % 60
  return `${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`
}

// 获取标签列表
const getTags = (data?: RelatedItemData): string[] => {
  if (!data) return []
  const tags: string[] = []

  // 添加分类标签
  if (data.category) {
    if (data.library === 'DAILY_LIBRARY_TYPE') {
      tags.push(`#${data.category} / 开眼精选`)
    } else {
      tags.push(`#${data.category}`)
    }
  }

  // 如果有 tags 数组，也添加
  if (data.tags && Array.isArray(data.tags)) {
    data.tags.forEach((tag: any) => {
      const tagName = typeof tag === 'string' ? tag : tag.name
      if (tagName && !tags.includes(`#${tagName}`)) {
        tags.push(`#${tagName}`)
      }
    })
  }

  // 只返回前3个标签
  return tags.slice(0, 3)
}

// 处理点击
const handleItemClick = (item: RelatedItem) => {
  emit('item-click', item)
}
</script>

<style scoped lang="less">
.related-list {
  padding: 0;
  background: transparent;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 24px;
}

.video-grid {
  display: flex;
  flex-direction: column;
  gap: 3vh;
}

.video-item {
  display: flex;
  gap: 3vw;
  cursor: pointer;
  padding: 1vh 0;
  transition: background-color 0.2s;
  border-radius: 8px;

  &:hover {
    background-color: rgba(255, 255, 255, 0.05);
  }

  &:active {
    background-color: rgba(255, 255, 255, 0.08);
  }
}

.video-thumbnail {
  position: relative;
  width: 35vw;
  aspect-ratio: 16 / 9;
  flex-shrink: 0;
  border-radius: 8px;
  overflow: hidden;
  background-color: #2c2c2c;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .duration {
    position: absolute;
    bottom: 4px;
    right: 4px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 2px;
    font-weight: 500;
  }
}

.video-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 0.5vh 0;

  .video-title {
    font-size: 14px;
    font-weight: 500;
    color: #fff;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 1vh;
  }

  .video-author {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.6);
    margin-bottom: 0.5vh;
  }

  .video-tags {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.56);
    display: flex;
    flex-wrap: wrap;
    gap: 4px;

    .tag {
      display: inline-block;
    }
  }
}
</style>
